package org.wlgzs.shop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.wlgzs.shop.bean.Msg;
import org.wlgzs.shop.bean.UserDate;
import org.wlgzs.shop.service.IUserDateService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangyan
 */
@Api(tags = "这是登录的接口", value = "LoginController")

@Controller
public class LoginController {
    @Autowired
    IUserDateService userDateService;

    @RequestMapping("/jumpLoginUserPage")
    @ApiOperation(value = "用户登录页面跳转",httpMethod = "POST",response = String.class)
    public String loginUserPage() { return "/account/land"; }

    @RequestMapping("/jumpLoginManagerPage")
    @ApiOperation(value = "管理者登录页面跳转",httpMethod = "POST",response = String.class)
    public String loginManagerPage(){
        return "/backstageManagementSystem/backstage_logon";
    }

    @RequestMapping("/jumpLoginBusinessManPage")
    @ApiOperation(value = "店铺登录页面跳转",httpMethod = "POST",response = String.class)
    public String loginBusinessManPage(){
        return "/backstage_business/store_logon";
    }

    @RequestMapping("/jumpBusinessBackgronud")
    @ApiOperation(value = "登录身份错误的页面跳转",httpMethod = "POST",response = String.class)
    public String jumpBusinessBackgronud(){
        return "merchant/businessBackgronud";
    }

    @RequestMapping("/registeredjump")
    @ApiOperation(value = "注册跳转页面",httpMethod = "POST")
    public ModelAndView registeredJump() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/account/register");
        return modelAndView;
    }



    @RequestMapping("/exit")
    @ApiOperation(value = "注销用户")
    public String exitUser(HttpSession session){
       session.invalidate();
       return "/backstageManagementSystem/backstage_logon";
    }

    /**
     *
     * @param userDate
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/login")
    @ApiOperation(value = "用户登陆", notes = "不能为空",httpMethod = "POST",response =Msg.class )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userName",
                    value = "用户名", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "password",
                    value = "用户密码", required = true, dataType = "String"),
    }
    )
    public Msg loginAllUserController(UserDate userDate, HttpServletRequest request) {
        HttpSession session = request.getSession();
        if (userDateService.loginUser(userDate, session)) {
            return Msg.success().add("date","登录成功！");
        } else {
            return Msg.fail().add("date","登录失败!");
        }
    }



    @ResponseBody
    @RequestMapping(value = "/registered")
    @ApiOperation(value = "用户注册",
            httpMethod = "POST", response = Msg.class )
    public Msg registeredController(@Valid UserDate userDate, BindingResult result, @Param("verificationCode") String verificationCode, HttpSession session) {
        if (!session.getAttribute("checkNumber").equals(verificationCode)) {
            return Msg.fail().add("error", "邮箱验证失败！");
        }
        if (result.hasErrors()) {
            Map<String, Object> map = new HashMap<>();
            List<FieldError> errors = result.getFieldErrors();
            for (FieldError fieldError : errors) {
                map.put(fieldError.getField(), fieldError.getDefaultMessage());
            }
            return Msg.fail().add("errorFields", map);
        } else {
            if (userDateService.registeredInsertService(userDate)) {
                return Msg.success().add("date","注册成功！");
            } else {
                return new Msg().add("error", "用户名已存在！");
            }
        }
    }


}
